﻿namespace com.stifel.BranchMgmtDao
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using com.stifel.Extensions.SharePointExtensions;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Linq;

    using Context = BranchMgmtDataContext;

    using CustomObject = StifelBranchConfiguration;

    public class StifelBranchConfigurationDao : StifelBaseDao
    {
        #region Fields

        public const string ListName = "stifelbranchconfiguration";

        #endregion Fields

        #region Constructors

        public StifelBranchConfigurationDao( )
            : base(ListName)
        {
            // set to the site collection url
            CurrentWebUrl = SPContext.Current.Site.RootWeb.Url;
        }

        public StifelBranchConfigurationDao( string currentWebUrl )
            : base(ListName)
        {
            // set to the site collection url
            using ( SPSite spSite = new SPSite( currentWebUrl ) )
            {
                using ( SPWeb spWeb = spSite.RootWeb )
                {
                    CurrentWebUrl = spWeb.Url;
                }
            }
        }

        #endregion Constructors

        #region Methods

        public CustomObject Add( CustomObject value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                string title = value.Title;
                List<SPFile> attachments = value.Attachments;
                Guid g = Guid.NewGuid( );
                value.Title = g.ToString( );
                l.InsertOnSubmit( clean( value ) );
                c.SubmitChanges( );
                CustomObject v = ( from o in l
                                   where o.Title == g.ToString( )
                                   select o ).SingleOrDefault( );

                v.Title = title;
                c.SubmitChanges( );

                if ( attachments != null && attachments.Count > 0 )
                    v.ListItem.AddAttachmentNow( attachments );

                return v;
            }
        }

        public void Clear( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                var v = ( from o in l
                          where o.Id > 0
                          select o );
                if ( v.Count( ) > 0 )
                {
                    l.DeleteAllOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public CustomObject Get( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                List<CustomObject> v = ( from o in l
                                         select o ).ToList( );
                return v.Count > 0 ? v[ 0 ] : null;
            }
        }

        public List<CustomObject> GetAll( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                List<CustomObject> l = c.GetList<CustomObject>( ListTitle ).ToList( );
                return l;
            }
        }

        CustomObject clean( CustomObject value )
        {
            return new CustomObject
            {
                BeginNotificationNDaysBeforeBranchHardwareScheduledMaintenance = value.BeginNotificationNDaysBeforeBranchHardwareScheduledMaintenance,
                BeginNotificationNDaysBeforeBranchLeaseExpiration = value.BeginNotificationNDaysBeforeBranchLeaseExpiration,
                BeginNotificationNDaysBeforeBranchTelMaintenanceContractExpiration = value.BeginNotificationNDaysBeforeBranchTelMaintenanceContractExpiration,
                BranchHardwareMaintenanceGroupId = value.BranchHardwareMaintenanceGroupId,
                BranchHardwareMaintenanceScheduledNotificationTemplate = value.BranchHardwareMaintenanceScheduledNotificationTemplate,
                BranchHardwareMaintenanceScheduledNotificationTitle = value.BranchHardwareMaintenanceScheduledNotificationTitle,
                BranchHardwareMaintenanceSendEmail = value.BranchHardwareMaintenanceSendEmail,
                BranchLeaseExpirationNotificationTemplate = value.BranchLeaseExpirationNotificationTemplate,
                BranchLeaseExpirationNotificationTitle = value.BranchLeaseExpirationNotificationTitle,
                BranchLeaseGroupId = value.BranchLeaseGroupId,
                BranchLeaseSendEmail = value.BranchLeaseSendEmail,
                BranchTelMaintenanceContractExpirationNotificationTemplate = value.BranchTelMaintenanceContractExpirationNotificationTemplate,
                BranchTelMaintenanceContractExpirationNotificationTitle = value.BranchTelMaintenanceContractExpirationNotificationTitle,
                BranchTelMaintenanceContractGroupId = value.BranchTelMaintenanceContractGroupId,
                BranchTelMaintenanceContractSendEmail = value.BranchTelMaintenanceContractSendEmail,
                ProductionSiteURLForTheLiveBranches = value.ProductionSiteURLForTheLiveBranches,
                RepeatNotificationEveryNDaysUntilBranchLeaseResolution = value.RepeatNotificationEveryNDaysUntilBranchLeaseResolution,
                RepeatNotificationEveryNDaysUntilBranchTelMaintenanceContractResolution = value.RepeatNotificationEveryNDaysUntilBranchTelMaintenanceContractResolution,
                RepeatNotificationEveryNDaysUntilScheduleBranchHardwareMaintenanceCompleted = value.RepeatNotificationEveryNDaysUntilScheduleBranchHardwareMaintenanceCompleted,
                SiteURLForTheBranchHardwareMaintenanceNotification = value.SiteURLForTheBranchHardwareMaintenanceNotification,
                SiteURLForTheBranchLeaseNotification = value.SiteURLForTheBranchLeaseNotification,
                SiteURLForTheBranchTelMaintenanceContractNotification = value.SiteURLForTheBranchTelMaintenanceContractNotification,
                Title = value.Title
            };
        }

        #endregion Methods
    }
}